<template>
  <div>正在登录……</div>
</template>
<script>
import { sign, signStr } from "@/assets/js/sign.js";
import { getWXLoginUrl, isWeiXin, urlParse } from "@/assets/js/utils"; //获取url参数
export default {
  data() {
    return {
      msg: "正在登录……"
    };
  },
  methods: {
    init() {
      if (this.$store.getters.getMemberPtId == 0) {
        let openid = this.$route.params.openid;
        // alert(openid);
        if (
          openid == null ||
          openid == "" ||
          openid == undefined ||
          openid == "null"
        ) {
          let stateurl = window.location.href;
          let url = getWXLoginUrl(stateurl);
          window.location.href = url;
          return false;
        } else {
          // this.MemberLogin(openid);
            this.$store.commit("setOpenid",openid);
            let params = urlParse();
            if (params != null && params.hasOwnProperty("code")) {
              this.getInfo(params["code"]);
            } else {
              let url = localStorage.getItem("beforeUrl");
              localStorage.removeItem("beforeUrl");
              if (
                url == null ||
                url == "" ||
                url == undefined ||
                url == "null"
              ) {
                this.msg = "无法定位到商家";
              } else {
                this.$router.push(url);
              }
            }
        }
      }
    },
    
    getInfo(code) {
      let urlparam = sign("/v1/api/FindQrCode");
      urlparam.append("QrCode", code);
      this.$http.post("/FindQrCode", urlparam).then(ret => {
        if (ret.status == 200) {
          var _data = ret.data.resultObject;
          var _code = ret.data.code || ret.data.result.code;
          var _msg = ret.data.msg || ret.data.result.msg;

          if (_code == 1) {
            //存储起来
            //存储商家信息
            this.$store.commit("addSellerInfo", _data.Seller);
            //存储餐桌信息
            this.$store.commit("addCzInfo", _data.BcanzwhVo);

            let url = localStorage.getItem("beforeUrl");
            localStorage.removeItem("beforeUrl");
            if (url == null || url == "" || url == undefined) {
              this.$router.push("/Seller/" + _data.Seller.id + "/0");
            } else {
              this.$router.push(url);
            }
          } else {
          }
        } else {
          this.$vux.toast.text(ret.data.msg, "top");
        }
      });
    }
  },
  watch: {
    $route(to, from) {
      if (to.name == "Wxlogin" && this.$store.getters.getMemberPtId == 0) {
        
        this.init();
      }
      if (this.$store.getters.getMemberPtId != 0 && to.name == "Wxlogin") {
        WeixinJSBridge.call("closeWindow");
       
        return false;
      }
    }
  },
  created() {
   this.init();
  }
};
</script>

